#!/bin/bash

# verify the DHCPD configuration file  (/etc/dhcpd.conf)
#
# Usage:
#    verifyDhcpdConf  <someLanIsADhcpServer>
#
# Description:
#    Make sure DHCPD_CONF_FILE exists if we need it
#    Call this script with the parm letting it know if we need the conf file
#    If we need it and it exists, do nothing, but if it is needed and doesn't
#    exist yet, copy the one from our shipped sample (/$HWMCAHOME/data/network/dhcpd.conf).
#    If it isn't needed,but exists already, erase it.
#
# Return Codes
#    0: Conf file not needed; doesn't exist; do nothing
#    1: Conf file needed; it already exists
#    2: Conf file needed; we copied it ok
#    3: Conf file no longer needed; we erased it ok
#  100: An error occurred
#
#
# Module History
#    00  11/11/2003  T. Forties  - Initial Release
#    01  02/16/2004  TF  - Copy sample config file correctly for P-series
#        02/16/2004  TF  - touch dhcpd.leases before starting dhcpd - Needed 1st time; won't hurt anytime

dhcp_conf_needed=$1
system_conf_file="/etc/dhcpd.conf"
z_sample_conf_file="$HWMCAHOME/data/network/dhcpd.conf"
p1_sample_conf_file="$CONSOLE_PATH/data/network/dhcpd.conf"
p2_sample_conf_file="/opt/ccfw/data/network/dhcpd.conf"

echo 'dhcp_conf_needed ='"$dhcp_conf_needed"
rc=100


# subroutine for checking if there were errors while collecting data
check_err() {
   if [ -s err.tmp ]; then
      echo "errors follow."
      cat -n err.tmp
      rc=100
   else
      echo "done."
   fi
   rm -f err.tmp
   echo ""
}



if  "$dhcp_conf_needed"
then
   echo 'DHCPD Conf file is needed'
   echo 'touch /var/lib/dhcp/dhcpd.leases'
   touch /var/lib/dhcp/dhcpd.leases
   if [ -e $system_conf_file ]; then
      echo 'System conf file exists'
      rc=1
   else
      echo 'System conf file does NOT exist'
      if [ -e $p1_sample_conf_file ]; then
         echo 'Use P-series sample conf file(1)'         
         cp "$p1_sample_conf_file" "$system_conf_file" 2>err.tmp
      else 
         if [ -e $p2_sample_conf_file ]; then
            echo 'Use P-series sample conf file(2)'         
            cp "$p2_sample_conf_file" "$system_conf_file" 2>err.tmp
         else
            echo 'Use Z-series sample conf file'         
            cp "$z_sample_conf_file" "$system_conf_file" 2>err.tmp      
         fi   
      fi             
      rc=2
      check_err
      echo 'Sample config file copied to System config file'

   fi

else
   echo 'DHCPD Conf file is NOT needed'
   if [ -e $system_conf_file ]; then
      echo 'System conf file exists - no longer needed'
      rm  "$system_conf_file" 2>err.tmp
      rc=3
      check_err
      echo 'System dhcpd config file erased'

   else
      echo 'System conf file does NOT exist - as expected'
      rc=0
   fi

fi
echo 'Return code = '"$rc"
exit "$rc"

